package com.woniu.custom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.utils.PageParam;
import com.woniu.common.utils.ResponseData;
import com.woniu.common.utils.ResponseEnum;
import com.woniu.common.utils.SnowflakeIdGenerator;
import com.woniu.custom.dto.CustomDetailDto;
import com.woniu.custom.dto.CustomDto;
import com.woniu.custom.dto.KefuDto;
import com.woniu.custom.dto.MessageDto;
import com.woniu.custom.mapper.*;
import com.woniu.custom.pojo.*;
import com.woniu.custom.service.MessageService;
import com.woniu.custom.service.ZyCustomService;
import com.woniu.custom.service.ZyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xue
 * @description 针对表【zy_custom】的数据库操作Service实现
 * @createDate 2024-08-07 15:39:40
 */
@Service
public class ZyCustomServiceImpl extends ServiceImpl<ZyCustomMapper, ZyCustom>
        implements ZyCustomService {


    @Autowired
    private ZyCustomMapper customMapper;
    @Autowired
    private ZyUserService userService;
    @Autowired
    private ZyCompanyMapper companyMapper;
    @Autowired
    private ChatServiceImpl chatService;
    @Autowired
    private MessageService messageService;

    @Override
    public ResponseData<?> queryCustomPage(PageParam<CustomDto> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<CustomDto> customDtos = customMapper.queryCustomPage(pageParam.getParam());
        PageInfo pageInfo = new PageInfo(customDtos);
        return new ResponseData<>().success(pageInfo);
//
    }

    @Override
    public ResponseData<?> laHei(String[] ids) {
        Integer aaa = 0;

        for (String id : ids) {
            boolean update = lambdaUpdate().
                    eq(ZyCustom::getId, id)
                    .set(ZyCustom::getCustomStates, 3)
                    .update();
            if (!update) {
                aaa += 1;
            }
        }
        if (aaa == 0) {
            return new ResponseData<>().success();
        }
        return new ResponseData<>().fail();
    }

    /**
     * @param id 客户id
     * @return
     */
    @Override
    public ResponseData<?> queryCustomDetail(String id) {
        //顾客详情=顾客表+公司表+用户表
        ZyCustom custom = getById(id);
        String userId = custom.getUserId();

        String customId = custom.getId();
        List<ZyCompany> zyCompanies = companyMapper.queryCompanyByUserId(customId);

        ZyUser user = userService.getById(userId);
        CustomDetailDto customDetailDto = new CustomDetailDto();
        customDetailDto.setCustom(custom);
        customDetailDto.setCompany(zyCompanies);
        customDetailDto.setUser(user);

        return new ResponseData<>().success(customDetailDto);
    }

    @Override
    public ResponseData<?> saveAll(KefuDto kefuDto) throws InterruptedException {
        //先得到用户信息
        String userPhone = null;
        ZyCustom zyCustom = null;
        String customId = null;
        try {
            userPhone = kefuDto.getUserPhone();
            zyCustom = lambdaQuery().eq(ZyCustom::getCustomPhone, userPhone).one();
            customId = zyCustom.getId();
        } catch (Exception e) {
            return new ResponseData<>().fail(ResponseEnum.PHONE_ERROR);
        }

        //添加chat
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
        long l = snowflakeIdGenerator.nextId();
        Chat chat = new Chat();
        chat.setMessageId(1L);
        chat.setId(String.valueOf(l));
        chat.setClientId(Long.valueOf(customId));
        chat.setClientName(zyCustom.getCustomName());
        //updateUser字段为phone
        chat.setUpdateUser(userPhone);
        chat.setAgentName( kefuDto.getUserAddr());//地址字段
        chat.setState(kefuDto.getUserQueryType());//客户诉求类型
        chat.setCreateUser(kefuDto.getTextarea());//名称代替详情
        try {
            chatService.saveAll(chat);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData<>().fail(ResponseEnum.SYSTEM_OUT);
        }
        //存储对话信息
        List<MessageDto> messageDto = kefuDto.getMessageDto();
        if (messageDto == null){
            return new ResponseData<>().fail(ResponseEnum.MESSAGE_NULL);
        }
        for (MessageDto dto : messageDto) {
            Message message = new Message();
            message.setContent(dto.getContent());
            message.setChatId(l);
            message.setType(dto.getType());
            messageService.save(message);
        }
        return new ResponseData<>().success();
    }

}




